package com.zhumei.keyscale.util;

import java.math.*;
import java.text.*;
import java.util.*;

public class MathUtils
{
    public static double bigDecimalAdd(final double n, final double n2) {
        return new BigDecimal(Double.toString(n)).add(new BigDecimal(Double.toString(n2))).setScale(2, RoundingMode.HALF_UP).doubleValue();
    }
    
    public static double bigDecimalAddExact(final double n, final double n2) {
        return new BigDecimal(Double.toString(n)).add(new BigDecimal(Double.toString(n2))).doubleValue();
    }
    
    public static double bigDecimalDivide(final double n, final double n2) {
        return new BigDecimal(Double.toString(n)).divide(new BigDecimal(Double.toString(n2)), 2, RoundingMode.HALF_UP).doubleValue();
    }
    
    public static double bigDecimalDivideExact(final double n, final double n2) {
        return new BigDecimal(Double.toString(n)).divide(new BigDecimal(Double.toString(n2))).doubleValue();
    }
    
    public static double bigDecimalMultiply(final double n, final double n2) {
        return new BigDecimal(Double.toString(n)).multiply(new BigDecimal(Double.toString(n2))).setScale(2, RoundingMode.HALF_UP).doubleValue();
    }
    
    public static double bigDecimalRemainder(final double n, final double n2) {
        return new BigDecimal(Double.toString(n)).remainder(new BigDecimal(Double.toString(n2))).doubleValue();
    }
    
    public static double bigDecimalSubtract(final double n, final double n2) {
        return new BigDecimal(Double.toString(n)).subtract(new BigDecimal(Double.toString(n2))).setScale(2, RoundingMode.HALF_UP).doubleValue();
    }
    
    public static double bigDecimalSubtract2(final double n, final int n2) {
        return new BigDecimal(Double.toString(n)).subtract(new BigDecimal(Double.toString(n2))).doubleValue();
    }
    
    public static String daofen(final double n) {
        String format;
        if ((format = new DecimalFormat("0.00").format(new BigDecimal(n).setScale(2, 4).doubleValue())).contains("-")) {
            format = "";
        }
        return format;
    }
    
    public static String daojiao(final double n) {
        String format;
        if ((format = new DecimalFormat("0.00").format(new BigDecimal(n).setScale(1, 4).doubleValue())).contains("-")) {
            format = "";
        }
        return format;
    }
    
    public static String daoyuan(final double n) {
        String format;
        if ((format = new DecimalFormat("0.00").format(new BigDecimal(n).setScale(0, 4).doubleValue())).contains("-")) {
            format = "";
        }
        return format;
    }
    
    public static String formatFileSize(final long n, final boolean b) {
        final double n2 = (double)n;
        final double log = Math.log(n2);
        final double n3 = 1024;
        final int n4 = (int)(log / Math.log(n3));
        double n5;
        if (n > 1024L) {
            n5 = n2 / Math.pow(n3, n4);
        }
        else {
            n5 = n2 / n3;
        }
        if (b) {
            return String.format(Locale.ENGLISH, "%.1f%s", n5, (new String[] { " B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB" })[n4]);
        }
        return String.format(Locale.ENGLISH, "%.1f", n5);
    }
    
    public static String formatKbps(final long n) {
        final StringBuilder sb = new StringBuilder();
        sb.append(new DecimalFormat("0.000").format(bigDecimalDivideExact((double)n, 1024.0)));
        sb.append("KB/s");
        return sb.toString();
    }
    
    public static String formatProgressValue(final Object o) {
        return new DecimalFormat("0.0%").format(o);
    }
    
    public static String formatSeekbarValue(final Object o) {
        return new DecimalFormat("0%").format(o);
    }
    
    public static String keepThreeDecimalDigits(final double n) {
        return new DecimalFormat("0.000").format(n);
    }
    
    public static String keepTwoDecimalDigits(final double n) {
        String format;
        if ((format = new DecimalFormat("0.00").format(n)).contains("-")) {
            format = "";
        }
        return format;
    }
    
    public static String keepTwoDecimalDigits(String format) {
        if ((format = new DecimalFormat("0.00").format(format)).contains("-")) {
            format = "";
        }
        return format;
    }
    
    public static String mofen(final double n) {
        final String format = new DecimalFormat("0.00").format(new BigDecimal(n).setScale(1, 1).doubleValue());
        if (format.contains("-")) {
            return "0.00";
        }
        return format;
    }
    
    public static String mojiao(final double n) {
        final String format = new DecimalFormat("0.00").format(new BigDecimal(n).setScale(0, 1).doubleValue());
        if (format.contains("-")) {
            return "0.00";
        }
        return format;
    }
    
    public static String noKeepDecimalDigits(final double n) {
        String format;
        if ((format = new DecimalFormat("0").format(n)).contains("-")) {
            format = "";
        }
        return format;
    }
}
